package com.mengfou.appserver.app;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;

import com.mengfou.appserver.app.thread.ExecutorPool;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/**
 * @author 梦否 on 2024/1/6
 * @blog https://mengfou.blog.csdn.net/
 */
public class IpAddressUtil {

    private static List<String> ipAddressList;

    public static void getLocalIpList() {
        if (ipAddressList != null) {
            return;
        }
        ExecutorPool.execute(new Runnable() {
            @Override
            public void run() {
                List<String> ipList = new ArrayList<>();
                try {
                    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
                    while (interfaces.hasMoreElements()) {
                        NetworkInterface networkInterface = interfaces.nextElement();

                        if (!networkInterface.isLoopback() && !networkInterface.isVirtual()) {
                            // 遍历每个网络接口的IP地址
                            for (Enumeration<InetAddress> addresses = networkInterface.getInetAddresses(); addresses.hasMoreElements(); ) {
                                InetAddress address = addresses.nextElement();

                                if (address instanceof Inet4Address) {
                                    Log.e("MainActivity", "IPv4");
                                } else {
                                    Log.e("MainActivity", "IPv6");
                                }

                                if (!address.equals(InetAddress.getLocalHost())) {
                                    String ipAddress = address.getHostAddress();
                                    Log.e("MainActivity", "IP Address: " + ipAddress);
                                    ipList.add(ipAddress);
                                }
                            }
                        }
                    }
                } catch (SocketException | UnknownHostException ex) {
                    throw new RuntimeException(ex);
                }
                ipAddressList = ipList;
            }
        });
    }


    /**
     * 获取本机IPv4地址
     *
     * @param context
     * @return 本机IPv4地址；null：无网络连接
     */
    public static String getIpAddress(Context context) {
        // 获取WiFi服务
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        // 判断WiFi是否开启
        if (wifiManager.isWifiEnabled()) {
            // 已经开启了WiFi
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            int ipAddress = wifiInfo.getIpAddress();
            String ip = intToIp(ipAddress);
            return ip;
        } else {
            // 未开启WiFi
            return getIpAddress();
        }
    }

    private static String intToIp(int ipAddress) {
        return (ipAddress & 0xFF) + "." +
                ((ipAddress >> 8) & 0xFF) + "." +
                ((ipAddress >> 16) & 0xFF) + "." +
                (ipAddress >> 24 & 0xFF);
    }

    /**
     * 获取本机IPv4地址
     *
     * @return 本机IPv4地址；null：无网络连接
     */
    private static String getIpAddress() {
        try {
            NetworkInterface networkInterface;
            InetAddress inetAddress;
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                networkInterface = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = networkInterface.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()) {
                        return inetAddress.getHostAddress();
                    }
                }
            }
            return null;
        } catch (SocketException ex) {
            ex.printStackTrace();
            return null;
        }
    }
}
